import {
    BASE_URL
} from '../../../http/config';

Page({
    data: {
        consultDetails: null, // 咨询师详情
        isLoading: false, // 加载状态
        consultId: null, // 咨询师ID
        articles: [], // 咨询师相关文章
        showCertificate: false ,// 控制证书弹窗显示
        courses:[]
    },

    /**
     * 获取咨询师详情
     */
    getConsultDetails(id) {
        if (!id) {
            wx.showToast({
                title: '参数错误，无法获取详情',
                icon: 'none'
            });
            return;
        }

        this.setData({
            isLoading: true
        });

        wx.request({
            url: `${BASE_URL}/consult/${id}`,
            method: 'GET',
            success: (res) => {
                if (res.data.code === 200) {
                    const details = res.data.data;
                    const stars = [];
                    for (let i = 0; i < 5; i++) {
                        stars.push(i < details.consultGrade ? 1 : 0); // 1 表示黄星星，0 表示灰星星
                    }

                    const processedDetails = {
                        ...details,
                        avatarUrl: details.avatarUrl,
                        certImg: details.certImg ,
                        stars
                    };

                    this.setData({
                        consultDetails: processedDetails
                    });

                    wx.setNavigationBarTitle({
                        title: processedDetails.name || '咨询师详情'
                    });
                } else {
                    wx.showToast({
                        title: res.data.message || '获取详情失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                wx.showToast({
                    title: '网络错误，请稍后重试',
                    icon: 'none'
                });
                console.error('获取咨询师详情失败：', err);
            },
            complete: () => {
                this.setData({
                    isLoading: false
                });
            }
        });
    },

    /**
     * 获取相关文章
     */
   fetchArticles(consultId) {
       wx.request({
           url: `${BASE_URL}/article/findArticlePage`,
           method: 'GET',
           data: {
               pageNum: 1,
               pageSize: 10,
               consultId: consultId
           },
           success: (res) => {
               if (res.data && res.data.code === 200) {
                   let articleList = res.data.data.list || [];

                   // 拼接每篇文章的图片 URL
                   articleList = articleList.slice(0, 3).map(article => {
                       return {
                           ...article,
                           url: article.url  // 处理为完整路径
                       };
                   });

                   this.setData({
                       articles: articleList
                   });
               } else {
                   wx.showToast({
                       title: res.data.message || '文章加载失败',
                       icon: 'none'
                   });
               }
           },
           fail: (err) => {
               console.error('请求文章失败', err);
               wx.showToast({
                   title: '网络异常，加载文章失败',
                   icon: 'none'
               });
           }
       });
   },
    fetchCourses(consultId) {
        wx.request({
            url: `${BASE_URL}/course/all`,
            method: 'GET',
            data: {
                pageNum: 1,
                pageSize: 10,
                consultId: consultId
            },
            success: (res) => {
                if (res.data && res.data.code === 200) {
                    let courseList = res.data.data.list || [];

                    // 拼接每篇文章的图片 URL
                    courseList = courseList.slice(0, 3).map(course => {
                        return {
                            ...course,
                            url: course.thumbnail  // 处理为完整路径
                        };
                    });

                    this.setData({
                        courses: courseList
                    });
                } else {
                    wx.showToast({
                        title: res.data.message || '文章加载失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.error('请求文章失败', err);
                wx.showToast({
                    title: '网络异常，加载文章失败',
                    icon: 'none'
                });
            }
        });
    
},

    /**
     * 页面加载时触发
     */
    onLoad(options) {
        const consultId = options.id || null;
        console.log('接收的咨询师ID：', consultId);

        if (consultId) {
            this.setData({
                consultId
            });
            this.getConsultDetails(consultId);
            this.fetchArticles(consultId);
            this.fetchCourses(consultId);
        } else {
            wx.showToast({
                title: '无效咨询师ID',
                icon: 'none'
            });
        }
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
        const {
            consultId
        } = this.data;
        if (consultId) {
            this.getConsultDetails(consultId);
            this.fetchArticles(consultId);
            this.fetchCourses(consultId);
        }
        wx.stopPullDownRefresh();
    },

    /**
     * 分享功能
     */
    onShareAppMessage() {
        const {
            consultDetails
        } = this.data;
        return {
            title: consultDetails?.name ? `${consultDetails.name}的咨询详情` : '咨询师详情',
            path: `/pages/consult/detail/detail?id=${consultDetails?.id}`
        };
            },

            /**
             * 查看证书
             * 点击"查看证书"按钮时触发，显示证书图片弹窗
             */
            viewCertificate() {
                    const {
                        consultDetails
                    } = this.data;

                    if (consultDetails && consultDetails.certImg) {
                        this.setData({
                            showCertificate: true
                        });
                    } else {
                        wx.showToast({
                            title: '暂无证书图片',
                            icon: 'none'
                        });
                    }
                },

                /**
                 * 关闭证书弹窗
                 */
                closeCertificate() {
                    this.setData({
                        showCertificate: false
                    });
    }
});